package net.int0r.gscore.gobjects;

import java.util.ArrayList;
import java.util.Iterator;

import net.int0r.gscore.util.GJSONObject;

import org.json.JSONArray;

public class Spot {
	private ArrayList<User> founders = new ArrayList<User>();
	private User creator;
	private int checkinCount = 0;
	private String name = "";
	private String itemsUrl = "";
	private int radiusMeters;
	private String twitterName = "";
	private boolean strictRadius = false;
	private double lat = 0.0;
	private int maxItemsCount = 0;
	private String checkinsUrl = "";
	private ArrayList<User> top10 = new ArrayList<User>();
	private double lng = 0.0;
	private String url = "";
	private String createdAt = "";
	private int visitorsCount = 0;
	private String desc = "";
	private int trendingLevel = 0;
	private String activityUrl = "";
	private String imageUrl = "";
	
	
	public String getImageUrl() {
		return imageUrl;
	}
	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}
	public String getItemsUrl() {
		return itemsUrl;
	}
	public void setItemsUrl(String itemsUrl) {
		this.itemsUrl = itemsUrl;
	}
	public int getRadiusMeters() {
		return radiusMeters;
	}
	public void setRadiusMeters(int radiusMeters) {
		this.radiusMeters = radiusMeters;
	}
	public String getTwitterName() {
		return twitterName;
	}
	public void setTwitterName(String twitterName) {
		this.twitterName = twitterName;
	}
	public boolean isStrictRadius() {
		return strictRadius;
	}
	public void setStrictRadius(boolean strictRadius) {
		this.strictRadius = strictRadius;
	}
	public double getLat() {
		return lat;
	}
	public void setLat(double lat) {
		this.lat = lat;
	}
	public int getMaxItemsCount() {
		return maxItemsCount;
	}
	public void setMaxItemsCount(int maxItemsCount) {
		this.maxItemsCount = maxItemsCount;
	}
	public String getCheckinsUrl() {
		return checkinsUrl;
	}
	public void setCheckinsUrl(String checkinsUrl) {
		this.checkinsUrl = checkinsUrl;
	}
	public ArrayList<User> getTop10() {
		return top10;
	}
	public void setTop10(ArrayList<User> top10) {
		this.top10 = top10;
	}
	public double getLng() {
		return lng;
	}
	public void setLng(double lng) {
		this.lng = lng;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getCreatedAt() {
		return createdAt;
	}
	public void setCreatedAt(String createdAt) {
		this.createdAt = createdAt;
	}
	public int getVisitorsCount() {
		return visitorsCount;
	}
	public void setVisitorsCount(int visitorsCount) {
		this.visitorsCount = visitorsCount;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public int getTrendingLevel() {
		return trendingLevel;
	}
	public void setTrendingLevel(int trendingLevel) {
		this.trendingLevel = trendingLevel;
	}
	public String getActivityUrl() {
		return activityUrl;
	}
	public void setActivityUrl(String activityUrl) {
		this.activityUrl = activityUrl;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public ArrayList<User> getFounders() {
		return founders;
	}
	public void setFounders(ArrayList<User> founders) {
		this.founders = founders;
	}
	public User getCreator() {
		return creator;
	}
	public void setCreator(User creator) {
		this.creator = creator;
	}
	public int getCheckinCount() {
		return checkinCount;
	}
	public void setCheckinCount(int checkinCount) {
		this.checkinCount = checkinCount;
	}
	
	public Spot(GJSONObject js) {
		try {
			js.getInt("checkins_count");
			if(!js.isNull("creator"))
				creator = new User(new GJSONObject(js.getJSONObject("creator").toString()));
			if(!js.isNull("founders")) {
				JSONArray fndJs = js.getJSONArray("founders");

				for(int i=0; i<fndJs.length(); i++) {
					founders.add(new User(new GJSONObject(fndJs.getJSONObject(i).toString())));
				}
			}
			setName(js.getString("name"));
			setItemsUrl(js.getString("items_url"));
			setRadiusMeters(js.getInt("radius_meters"));
			setTwitterName(js.getString("twitter_username"));
			setImageUrl(js.getString("image_url"));
			setStrictRadius(js.getBoolean("strict_radius"));
			setLat(js.getDouble("lat"));
			setCheckinsUrl(js.getString("checkins_url"));
			setMaxItemsCount(js.getInt("max_items_count"));
			if(!js.isNull("top_10")) {
				JSONArray fndJs = js.getJSONArray("top_10");

				for(int i=0; i<fndJs.length(); i++) {
					top10.add(new User(new GJSONObject(fndJs.getJSONObject(i).toString())));
				}
			}
			setLng(js.getDouble("lng"));
			setUrl(js.getString("url"));
			setCreatedAt(js.getString("created_at"));
			setDesc(js.getString("description"));
			setTrendingLevel(js.getInt("trending_level"));
			setActivityUrl(js.getString("activity_url"));
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public boolean isFounder(int userId) {
		Iterator<User> fndIt = founders.iterator();
		User tmp = null;
		while(fndIt.hasNext()) {
			tmp = fndIt.next();
			if(tmp.getUserId()==userId)
				return true;
		}
		return false;
	}
	
	public boolean isCreator(int userId) {
		if(creator.getUserId() == userId)
			return true;
		return false;
	}
	
	public String toString() {
		return getName();
	}
}
